import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 86184
 * Date: 2023-01-29
 * Time: 12:42
 */
public class Array_practice {
    //8.数组逆序
    public static int[] reverse(int[] tmp8){
        int left=0;
        int right=tmp8.length-1;

        while(left<right){
            int temp=tmp8[left];
            tmp8[left]=tmp8[right];
            tmp8[right]=temp;
            left++;
            right--;
        }
        return tmp8;
    }
    public static void main8(String[] args) {
        int[] array8={1, 2, 3, 4, 6, 74, 78, 79, 85, 98};

        int[] array88=reverse(array8);
        for (int i = 0; i <array88.length ; i++) {
            System.out.print(array88[i]+" ");
        }
    }

    //7. 数组排序(冒泡排序) (1)
    public static int[] bubbleSort(int[] tmp7){
        for (int i = 0; i <tmp7.length-1 ; i++) {
            int change=0;
            for (int j = 1; j <tmp7.length-i ; j++) {
                if(tmp7[j-1]>tmp7[j]){
                    change=1;
                    int temp=tmp7[j-1];
                    tmp7[j-1]=tmp7[j];
                    tmp7[j]=temp;
                }
            }
            if(change==0){
                break;
            }
        }
        return tmp7;
    }
    public static void main7(String[] args) {
        int[] array7={1,0,5,6,9,87,2,6,8,7,52,63};

        int[] array77=bubbleSort(array7);
        for (int i = 0; i <array77.length ; i++) {
            System.out.print(array77[i]+" ");
        }
    }

    //(2)排序（编译器自带）
    public static void main7_2(String[] args) {
        int[] array7={1,0,5,6,9,87,2,6,8,7,52,63};
        Arrays.sort(array7);
        System.out.println(Arrays.toString(array7));
    }

    //（3）编译器自带的二分查找
    public static void main7_3(String[] args) {
        int[] array7={1,0,5,6,9,87,2,6,8,7,52,63};
        Arrays.sort(array7);
        System.out.println(Arrays.binarySearch(array7,5));
    }


    //6.查找数组中指定元素(二分查找)
    public static void binarySearch(int x, int[] tmp6) {
        int left = 0;
        int right = tmp6.length - 1;

        while (left <= right) {
            int mid = (left + right) / 2;
            if (tmp6[right] == x) {
                System.out.println("找到了，下标为：" + right);
                return;
            } else if (tmp6[mid] == x) {
                System.out.println("找到了，下标为：" + mid);
                return;
            } else if (tmp6[left] == x) {
                System.out.println("找到了，下标为：" + left);
                return;
            }

            if (x > tmp6[mid]) {
                left = mid + 1;
            } else {
                right = mid - 1;
            }
        }
        System.out.println("不存在该元素");
    }
    public static void main6(String[] args) {
        int[] array6 = {1, 2, 3, 4, 6, 74, 78, 79, 85, 98};

        binarySearch(2, array6);
    }

    //5.查找数组中指定元素(顺序查找)
    public static void find(int x, int[] tmp5) {
        for (int i = 0; i < tmp5.length; i++) {
            if (tmp5[i] == x) {
                System.out.println("找到了,下标为：" + i);
                return;
            }
        }
        System.out.println("不存在垓元素");
    }

    public static void main5(String[] args) {
        int[] array5 = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

        find(10, array5);
    }

    //4.求数组中元素的平均值
    public static void main4(String[] args) {
        int[] array4 = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
        int sum = 0;
        for (int i = 0; i < array4.length; i++) {
            sum += array4[i];
        }
        System.out.println((double) sum / array4.length);
    }

    //3.数组拷贝
    //(1)
    public static int[] copy(int[] tmp3) {
        int[] array3 = new int[tmp3.length];
        for (int i = 0; i < tmp3.length; i++) {
            array3[i] = tmp3[i];
        }
        return array3;
    }

    public static void main3(String[] args) {
        int[] array3 = {1, 2, 3, 4, 5, 6, 7, 8, 9};

        int[] copy1 = copy(array3);
        for (int i = 0; i < copy1.length; i++) {
            System.out.print(copy1[i] + " ");
        }
    }
    //(2)全部拷贝
    public static void main3_2(String[] args) {
        int[] array3 = {1, 2, 3, 4, 5, 6, 7, 8, 9};
        //                         被拷贝的数组，     新数组的长度
        int[] array3_2=Arrays.copyOf(array3,array3.length*2);
        System.out.println(Arrays.toString(array3));
        System.out.println(Arrays.toString(array3_2));
    }
    //(3)范围拷贝
    public static void main3_3(String[] args) {
        int[] array3 = {1, 2, 3, 4, 5, 6, 7, 8, 9};
        int[] array3_3=Arrays.copyOfRange(array3,2,5);//[2,5)

        System.out.println(Arrays.toString(array3));
        System.out.println(Arrays.toString(array3_3));
    }
    //(4)
    public static void main3_4(String[] args) {
        int[] array3 = {1, 2, 3, 4, 5, 6, 7, 8, 9};
        int[] copy=new int[array3.length*2];
        //          被拷贝的数组，被拷贝的数组的起始位置，拷贝的数组，拷贝的数组的起始位置，需要拷贝多长
        System.arraycopy(array3,0,copy,5,array3.length-2);
        System.out.println(Arrays.toString(array3));
        System.out.println(Arrays.toString(copy));
    }
    //(5)克隆
    public static void main3_5(String[] args) {
        int[] array3 = {1, 2, 3, 4, 5, 6, 7, 8, 9};
        int[] array3_5=array3.clone();//产生一个副本
        array3_5[2]=22;
        System.out.println(Arrays.toString(array3));
        System.out.println(Arrays.toString(array3_5));
    }

    //2.数组转字符串
    public static void main2(String[] args) {
        int[] array2 = {1, 2, 3, 4, 5, 6, 7};

        String ret = Arrays.toString(array2);
        System.out.println(ret);
    }


    //1.获取斐波那契数列的前N项
    public static int[] fib(int n) {
        if (n <= 0) {
            return null;
        }

        int[] array1 = new int[n];
        array1[0] = 1;
        array1[1] = 1;
        for (int i = 2; i < array1.length; i++) {
            array1[i] = array1[i - 1] + array1[i - 2];
        }

        return array1;
    }

    public static void main1(String[] args) {
        int[] array = fib(15);

        for (int i = 0; i < array.length; i++) {
            System.out.print(array[i] + " ");
        }
    }
}
